// dao/userDao.js
// ʵ����MySQL����
var mysql = require('mysql');
var $conf = require('../../conf/db');
var $util = require('../utils/util');
var $sql = require('../mapping/activeDetailSqlMapping');

// ʹ�����ӳأ���������
var pool = mysql.createPool($util.extend({}, $conf.mysql));

module.exports = {
    insert: function (req, res, callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.insert, [req.param('content'),req.param('endTime'), 0, 0,0,req.session.currentUser.id], function (err, result) {
                if (result.affectedRows >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);

                } else {
                    callback("no");
                }
                // �ͷ�����
                connection.release();
            });

        });
    },
    queryById: function (active, callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryById,[active.activeDetailId], function (err, result) {
                result = {
                    active: active,
                    detail: result[0]
                };
                callback(result);

                connection.release();
            })
        });
    },
    queryStatistic:function (req, callback) {
        var param = req.query || req.params;
        $util.queryHelper(req,$sql.queryStatistic, [param.activityNumber],  pool,function (result) {
            callback(result);
        })
    },
    updateRegisNum:function (req, activeDetailId,callback) {
        $util.queryHelper(req,$sql.updateRegisNum, [activeDetailId],  pool,function (result) {
            callback(result);
        })
    },
    /**
     * 修改参加人数
     * id 活动的编号
     * */
    updateAttendNum:function(req,id,callback){
        $util.queryHelper(req,$sql.updateAttendNum, id,  pool,function (result) {
            callback(result);
        })
    },


    insertInvite: function (req,res, callback) {
        var param = req.query || req.params;
        $util.queryHelper(req, $sql.insertInvite, [req.param('activeId')], pool, function (result) {
            callback(result);
        })
    },

    updateDetail:function (req, callback) {
        $util.queryHelper(req, $sql.updateDetail, [req.body.content, req.body.endTime, req.body.activeDetailId], pool, function (result) {
            callback(result);
        })
    },




    delActivity:function (req, res, callback) {
        pool.getConnection(function(err, connection) {
            //管理员删除活动
            connection.query($sql.del, [req], function(err, result) {
                if(result.affectedRows>=1) {
                    result = {
                        code: 200,
                        content:req,
                    };
                    callback(result);

                }else{
                    callback("no");
                }
                // �ͷ�����
                connection.release();
            });
        });

    },
};
